package de.szut.ita13sommerfeld.blockadventure.gameentities.blocks;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.LinkedList;

import de.szut.ita13sommerfeld.blockadventure.controller.Controller;
import de.szut.ita13sommerfeld.blockadventure.gameentities.Block;
import de.szut.ita13sommerfeld.blockadventure.gameentities.Entity;
import de.szut.ita13sommerfeld.blockadventure.gameentities.Missile;
import de.szut.ita13sommerfeld.blockadventure.gameentities.Player;
import de.szut.ita13sommerfeld.blockadventure.gameentities.blocks.missiles.AIM120;
import de.szut.ita13sommerfeld.blockadventure.gameentities.types.BlockType;
import de.szut.ita13sommerfeld.blockadventure.level.SpawnPoint;
import de.szut.ita13sommerfeld.blockadventure.util.Vector;

public class MissileLauncher extends Block {
	
	private BufferedImage[] tex;
	
	private int facing;
	private final int MIN_FIRE_DELAY = 10;
	private final int RANDOM_EXTRA = 50;
	private int ticker;
	private boolean test;
	
	public MissileLauncher(float x, float y, float width, float height, SpawnPoint spawnpoint, int facing) {
		super(x, y, width, height, spawnpoint);
		this.facing = facing;
		tex = new BufferedImage[2];
		tex[0] = texture.getBlockTexture(BlockType.MissileLauncherLeft);
		tex[1] = texture.getBlockTexture(BlockType.MissileLauncherRight);
	}

	public void render(Graphics g) {
		g.drawImage(tex[facing], (int)x, (int)y, null);
		g.setColor(Color.WHITE);
		g.drawString(String.valueOf(test), (int)x, (int)y - 10);
	}

	public void tick(LinkedList<Entity> entities) {
		ticker = (ticker + 1) % (random.nextInt(RANDOM_EXTRA) + MIN_FIRE_DELAY);
		float playerY = controller.getCharacter().getY();
		float playerX = controller.getCharacter().getX();
		facing = (Vector.isBehind(playerX, x, width)) ? 1 : 0;	
		test = Vector.onSameLevel(playerY, Player.HEIGHT, y, height);
		if(test && ticker == 0) {
			fireMissile();
		}
	}
	
	public Rectangle getBounds() {
		return new Rectangle((int)x, (int)y, (int)width, (int)height);
	}
	
	public Rectangle getBottomBounds() {
		return new Rectangle((int)x + 5, (int)y + (int)(height - 5), (int)width - 10, (int)5);
	}

	public Rectangle getBoundsTop() {
		return new Rectangle((int)x + 5, (int)y, (int)width - 10, (int)5);
	}

	public Rectangle getBoundsRight() {
		return new Rectangle((int)(x + width - 5), (int)y + 5, (int)5, (int)height - 10);
	}

	public Rectangle getBoundsLeft() {
		return new Rectangle((int)x, (int)y + 5, (int)5, (int)height - 10);
	}
	
	private void fireMissile() {
		if(facing == Missile.DIR_LEFT) {
			controller.addEntityToList(Controller.EntityList.ADD, new AIM120(x + width - Missile.WIDTH, 
					y + (width / 2) - Missile.HEIGHT / 2, Missile.WIDTH, Missile.HEIGHT,
					new SpawnPoint(x + width - Missile.WIDTH, y + (width / 2) - Missile.HEIGHT / 2),
					0.25f, 5, facing));
		} else if(facing == Missile.DIR_RIGHT) {
			controller.addEntityToList(Controller.EntityList.ADD, new AIM120(x, y + (width / 2) - Missile.HEIGHT / 2, 
					Missile.WIDTH, Missile.HEIGHT, new SpawnPoint(x + width - Missile.WIDTH, y + (width / 2) - Missile.HEIGHT / 2), 
					0.25f, 5, facing));
		}
	}

	public int getFacing() {
		return facing;
	}

	public void setFacing(int facing) {
		this.facing = facing;
	}
}
